const AV = require('leanengine')
const Promise = require('bluebird')

module.exports = {

  getByOpenid (openid) {
    var cql = `select * from _User where openid = '${openid}'`
    return AV.Query.doCloudQuery(cql).then(function (ret) {
      if (!ret.results.length) {
        return Promise.reject({
          msg: 'openid has not been registered'
        })
      } else {
        return Promise.resolve(ret.results[0])
      }
    })
  },

  getByPhone (phone) {
    var cql = `select * from _User where mobilePhoneNumber = '${phone}'`
    return AV.Query.doCloudQuery(cql).then(function (ret) {
      if (!ret.results.length) {
        return Promise.reject({
          msg: 'not exists'
        })
      } else {
        return Promise.resolve(ret.results[0])
      }
    })
  }



}
